home *** CD-ROM | disk | FTP | other *** search
/ com!online 2005 May / com_0505_1.iso / opensource / top10 / amc_install.exe / {app} / Scripts / FilmPub + IMDB (pic).ifs < prev    next >
Encoding:
Text File  |  2004-12-12  |  15.8 KB  |  552 lines

  1. // GETINFO SCRIPTING
  2. // IMDB (US) import with small picture
  3. // Filmpub (CZ) import, made by Marek Pospisil
  4.  
  5. (***************************************************
  6.  *  Movie importation script for:                  *
  7.  *      IMDB (US),    http://us.imdb.com           *
  8.  *      FilmPub (CZ), http://filmpub.atlas.cz      *
  9.  *                                                 *
  10.  *  (c) 2002-2004 Antoine Potten                   *
  11.  *                          software@antp.be       *
  12.  *  Contributors :                                 *
  13.  *    Danny Falkov                                 *
  14.  *    Kai Blankenhorn                              *
  15.  *                                                 *
  16.  *  For use with Ant Movie Catalog 3.4.0           *
  17.  *  www.antp.be/software/moviecatalog              *
  18.  *                                                 *
  19.  *  This program is free software; you can         *
  20.  *  redistribute it and/or modify it under the     *
  21.  *  terms of the GNU General Public License as     *
  22.  *  published by the Free Software Foundation;     *
  23.  *  either version 2 of the License, or (at your   *
  24.  *  option) any later version.                     *
  25.  ***************************************************)
  26.  
  27. program IMDbFilmPub;
  28.  
  29. const
  30.   DescriptionToImport = 2;
  31.    {
  32.       2 = import longest
  33.       1 = import short (from main page, faster)
  34.       0 = display list to select a description
  35.    }
  36.  
  37.  
  38.   BASEURL = 'http://filmpub.atlas.cz';
  39.   // true ... bude vyplneno, false .. nebude vyplneno
  40.   cOriginalTitle   = false;
  41.   cTranslatedTitle = true;
  42.   cDescription     = true;
  43.   cComments        = true;
  44.  
  45. var
  46.   MovieName: string;
  47.   MovieURL: string;
  48.  
  49. function FindLine(Pattern: string; List: TStringList; StartAt: Integer): Integer;
  50. var
  51.   i: Integer;
  52. begin
  53.   result := -1;
  54.   if StartAt < 0 then
  55.     StartAt := 0;
  56.   for i := StartAt to List.Count-1 do
  57.     if Pos(Pattern, List.GetString(i)) <> 0 then
  58.     begin
  59.       result := i;
  60.       Break;
  61.     end;
  62. end;
  63.  
  64. procedure AnalyzePage(Address: string);
  65. var
  66.   Page: TStringList;
  67. begin
  68.   Page := TStringList.Create;
  69.   Page.Text := GetPage(Address);
  70.   if pos('<title>IMDb', Page.Text) = 0 then
  71.   begin
  72.     AnalyzeMoviePage(Page)
  73.   end else
  74.   begin
  75.     PickTreeClear;
  76.     AddMoviesTitles(Page, '<b>Exact Matches</b>');
  77.     AddMoviesTitles(Page, '<b>Partial Matches</b>');
  78.     AddMoviesTitles(Page, '<b>Approximate Matches</b>');
  79.     if PickTreeExec(Address) then
  80.       AnalyzePage(Address);
  81.   end;
  82.   Page.Free;
  83. end;
  84.  
  85. procedure AnalyzeMoviePage(Page: TStringList);
  86. var
  87.   Line, Value, Value2, FullValue: string;
  88.   LineNr: Integer;
  89.   BeginPos, EndPos, DescrImport: Integer;
  90. begin
  91.   DescrImport := DescriptionToImport;
  92.   if (DescrImport <> 1) and (Pos('<a href="plotsummary">', Page.Text) = 0) then
  93.     DescrImport := 1;
  94.  
  95.   MovieURL := 'http://imdb.com/title/tt' + Copy(Page.Text, Pos('?pending&add=', Page.Text) + 17, 7);
  96.  
  97.   // URL
  98.   SetField(fieldURL, MovieURL);
  99.  
  100.   // Original Title & Year
  101.   LineNr := FindLine('<title>', Page, 0);
  102.   Line := Page.GetString(LineNr);
  103.   if LineNr > -1 then
  104.   begin
  105.     BeginPos := pos('<title>', Line);
  106.     if BeginPos > 0 then
  107.       BeginPos := BeginPos + 7;
  108.     EndPos := pos('(', Line);
  109.     if EndPos = 0 then
  110.       EndPos := Length(Line);
  111.     Value := copy(Line, BeginPos, EndPos - BeginPos - 1);
  112.     HTMLDecode(Value);
  113.     SetField(fieldOriginalTitle, Value);
  114.     BeginPos := pos('(', Line) + 1;
  115.     if BeginPos > 0 then
  116.     begin
  117.       EndPos := Pos('/I', Line);
  118.       if EndPos < BeginPos then
  119.         EndPos := Pos(')', Line);
  120.       Value := copy(Line, BeginPos, EndPos - BeginPos);
  121.       SetField(fieldYear, Value);
  122.     end;
  123.   end;
  124.  
  125.   // Rating
  126.   LineNr := FindLine('User Rating:', Page, 0);
  127.   if LineNr > -1 then
  128.   begin
  129.     Line := Page.GetString(LineNr + 4);
  130.     if Pos('/10', Line) > 0 then
  131.     begin
  132.       BeginPos := pos('<b>', Line) + 3;
  133.       Value := IntToStr(Round(StrToInt(StrGet(Line, BeginPos), 0) + (StrToInt(StrGet(Line, BeginPos + 2), 0) / 10)));
  134.       SetField(fieldRating, Value);
  135.     end;
  136.   end;
  137.  
  138.   // Picture
  139.   LineNr := FindLine('<img border="0" alt="cover"', Page, 0);
  140.   if LineNr > -1 then
  141.   begin
  142.     Line := Page.GetString(LineNr);
  143.     BeginPos := pos('src="', Line) + 4;
  144.     Delete(Line, 1, BeginPos);
  145.     EndPos := pos('"', Line);
  146.     Value := copy(Line, 1, EndPos - 1);
  147.     GetPicture(Value, False); // False = do not store picture externally ; store it in the catalog file
  148.   end;
  149.  
  150.   // Director
  151.   LineNr := FindLine('Directed by', Page, 0);
  152.   if LineNr > -1 then
  153.   begin
  154.     FullValue := '';
  155.     Line := Page.GetString(LineNr + 1);
  156.     repeat
  157.       BeginPos := pos('">', Line) + 2;
  158.       EndPos := pos('</a>', Line);
  159.       Value := copy(Line, BeginPos, EndPos - BeginPos);
  160.       if (Value <> '(more)') and (Value <> '') then
  161.       begin
  162.         if FullValue <> '' then
  163.           FullValue := FullValue + ', ';
  164.         FullValue := FullValue + Value;
  165.       end;
  166.       Delete(Line, 1, EndPos);
  167.     until Pos('</a>', Line) = 0;
  168.     HTMLDecode(FullValue);
  169.     SetField(fieldDirector, FullValue);
  170.   end;
  171.  
  172.   // Actors
  173.   LineNr := FindLine('ast overview', Page, 0);
  174.   if LineNr = -1 then
  175.     LineNr := FindLine('redited cast', Page, 0);
  176.   if LineNr > -1 then
  177.   begin
  178.     FullValue := '';
  179.     Line := Page.GetString(LineNr);
  180.     repeat
  181.       BeginPos := Pos('<td valign="top">', Line);
  182.       if BeginPos > 0 then
  183.       begin
  184.         Delete(Line, 1, BeginPos);
  185.         Line := copy(Line, 25, Length(Line));
  186.         BeginPos := pos('">', Line) + 2;
  187.         EndPos := pos('</a>', Line);
  188.         if EndPos = 0 then
  189.           EndPos := Pos('</td>', Line);
  190.         Value := copy(Line, BeginPos, EndPos - BeginPos);
  191.         if (Value <> '(more)') and (Value <> '') then
  192.         begin
  193.           BeginPos := pos('.... </td><td valign="top">', Line);
  194.           if BeginPos > 0 then
  195.           begin
  196.             EndPos := pos('</td></tr>', Line);
  197.             BeginPos := BeginPos + 27;
  198.             Value2 := copy(Line, BeginPos, EndPos - BeginPos);
  199.             if Value2 <> '' then
  200.             begin
  201.               Value := Value + ' (as ' + Value2 + ')';
  202.             end;
  203.           end;
  204.           if FullValue <> '' then
  205.             FullValue := FullValue + ', ';
  206.           FullValue := FullValue + Value;
  207.         end;
  208.         EndPos := Pos('</td></tr>', Line);
  209.         Delete(Line, 1, EndPos);
  210.       end else
  211.       begin
  212.         Line := '';
  213.       end;
  214.     until Line = '';
  215.     HTMLDecode(FullValue);
  216.     SetField(fieldActors, FullValue);
  217.   end;
  218.  
  219.   //Country
  220.   LineNr := FindLine('Country:', Page, 0);
  221.   if LineNr > -1 then
  222.   begin
  223.     Line := Page.GetString(LineNr + 1);
  224.     BeginPos := pos('/">', Line) + 3;
  225.     EndPos := pos('</a>', Line);
  226.     Value := copy(Line, BeginPos, EndPos - BeginPos);
  227.     HTMLDecode(Value);
  228.     SetField(fieldCountry, Value);
  229.   end;
  230.  
  231.   //Category
  232.   LineNr := FindLine('Genre:', Page, 0);
  233.   if LineNr > -1 then
  234.   begin
  235.     Line := Page.GetString(LineNr + 1);
  236.     BeginPos := pos('/">', Line) + 3;
  237.     EndPos := pos('</a>', Line);
  238.     Value := copy(Line, BeginPos, EndPos - BeginPos);
  239.     HTMLDecode(Value);
  240.     SetField(fieldCategory, Value);
  241.   end;
  242.  
  243.   //Description
  244.   LineNr := FindLine('Plot Summary:', Page, 0);
  245.   if LineNr < 1 then
  246.     LineNr := FindLine('Plot Outline:', Page, 0);
  247.   if LineNr > -1 then
  248.   begin
  249.     Line := Page.GetString(LineNr);
  250.     BeginPos := pos('</b>', Line) + 5;
  251.     EndPos := pos('<a href="/rg/', Line);
  252.     if EndPos < 1 then
  253.     begin
  254.         Line := Line + Page.GetString(LineNr+1);
  255.         EndPos := pos('<a href="/rg/', Line);
  256.         if EndPos < 1 then
  257.           EndPos := pos('<br><br>', Line);
  258.         if EndPos < 1 then
  259.           EndPos := Length(Line);
  260.     end;
  261.     Value := copy(Line, BeginPos, EndPos - BeginPos);
  262.     HTMLDecode(Value);
  263.     HTMLRemoveTags(Value);
  264.     case DescrImport of
  265.       0:
  266.         begin
  267.           PickListClear;
  268.           PickListAdd(Value);
  269.           GetDescriptions(GetField(fieldURL) + 'plotsummary');
  270.           if PickListExec('Select a description for "' + MovieName + '"', Value) then
  271.             SetField(fieldDescription, Value);
  272.         end;
  273.       1:
  274.         SetField(fieldDescription, Value);
  275.       2:
  276.         SetField(fieldDescription, GetDescriptions(MovieURL + 'plotsummary'));
  277.     end;
  278.   end;
  279.  
  280.   // Comments
  281.   LineNr := FindLine('<b>Summary:</b>', Page, 0);
  282.   if LineNr > -1 then
  283.   begin
  284.     Value := '';
  285.     repeat
  286.       LineNr := LineNr + 1;
  287.       Line := Page.GetString(LineNr);
  288.       EndPos := Pos('</blockquote>', Line);
  289.       if EndPos = 0 then
  290.         EndPos := Length(Line)
  291.       else
  292.         EndPos := EndPos - 1;
  293.       Value := Value + Copy(Line, 1, EndPos) + ' ';
  294.     until Pos('</blockquote>', Line) > 0;
  295.     HTMLDecode(Value);
  296.     Value := StringReplace(Value, '<br>', #13#10);
  297.     Value := StringReplace(Value, #13#10+' ', #13#10);
  298.     SetField(fieldComments, Value);
  299.   end;
  300.  
  301.   // Length
  302.   LineNr := FindLine('Runtime:', Page, 0);
  303.   if LineNr > -1 then
  304.   begin
  305.     Line := Page.GetString(LineNr + 1);
  306.     EndPos := pos(' min', Line);
  307.     if EndPos = 0 then
  308.       EndPos := pos('  /', Line);
  309.     if EndPos = 0 then
  310.       EndPos := Length(Line);
  311.     if Pos(':', Line) < EndPos then
  312.       BeginPos := Pos(':', Line) + 1
  313.     else
  314.       BeginPos := 1;
  315.     Value := copy(Line, BeginPos, EndPos - BeginPos);
  316.     SetField(fieldLength, Value);
  317.   end;
  318.  
  319.   // Language
  320.   LineNr := FindLine('Language:', Page, 0);
  321.   if LineNr > -1 then
  322.   begin
  323.     Line := Page.GetString(LineNr + 1);
  324.     BeginPos := pos('/">', Line) + 3;
  325.     EndPos := pos('</a>', Line);
  326.     if EndPos = 0 then
  327.       EndPos := Length(Line);
  328.     Value := copy(Line, BeginPos, EndPos - BeginPos);
  329.     SetField(fieldLanguages, Value);
  330.   end;
  331.  
  332. //  DisplayResults;
  333. end;
  334.  
  335. function GetDescriptions(Address: string): string;
  336. var
  337.   Line, Value: string;
  338.   LineNr: Integer;
  339.   BeginPos, EndPos,Longest: Integer;
  340.   Page: TStringList;
  341. begin
  342.   Result := '';
  343.   Longest := 0;
  344.   Page := TStringList.Create;
  345.   Page.Text := GetPage(Address);
  346.   LineNr := FindLine('<p class="plotpar">', Page, 0);
  347.   while LineNr > -1 do
  348.   begin
  349.     Value := '';
  350.     repeat
  351.       Line := Page.GetString(LineNr);
  352.       BeginPos := pos('"plotpar">', Line);
  353.       if BeginPos > 0 then
  354.         BeginPos := BeginPos + 10
  355.       else
  356.         BeginPos := 1;
  357.       EndPos := pos('</p>', Line);
  358.       if EndPos < 1 then
  359.         EndPos := Length(Line) + 1;
  360.       if Value <> '' then
  361.         Value := Value + ' ';
  362.       Value := Value + copy(Line, BeginPos, EndPos - BeginPos);
  363.       LineNr := LineNr + 1;
  364.     until (pos('</p>', Line) > 0) or (LineNr = Page.Count);
  365.     HTMLDecode(Value);
  366.     HTMLRemoveTags(Value);
  367.     PickListAdd(Value);
  368.  
  369.     if Length(Value) > Longest then
  370.     begin
  371.       Result := Value;
  372.       Longest := Length(Value);
  373.     end;
  374.  
  375.     LineNr := FindLine('<p class="plotpar">', Page, LineNr);
  376.   end;
  377.   Page.Free;
  378. end;
  379.  
  380. procedure AddMoviesTitles(Page: TStringList; Tag: string);
  381. var
  382.   Line: string;
  383.   LineNr: Integer;
  384.   MovieTitle, MovieAddress: string;
  385.   StartPos: Integer;
  386. begin
  387.   LineNr := FindLine(tag, Page, 0);
  388.   if LineNr > -1 then
  389.   begin
  390.     Line := Page.GetString(LineNr);
  391.     HTMLRemoveTags(Line);
  392.     PickTreeAdd(Trim(Line), '');
  393.     LineNr := LineNr + 5;
  394.     Line := Page.GetString(LineNr);
  395.     repeat
  396.       StartPos := pos('href="', Line) + 5;
  397.       Delete(Line, 1, StartPos);
  398.       MovieAddress := Copy(Line, 1, pos('">', Line) - 1);
  399.       StartPos := Pos('">', Line) + 2;
  400.       MovieTitle := Copy(Line, StartPos, Pos('</td>', Line) - StartPos);
  401.       HTMLRemoveTags(MovieTitle);
  402.       HTMLDecode(Movietitle);
  403.       PickTreeAdd(MovieTitle, 'http://us.imdb.com' + MovieAddress);
  404.       LineNr := LineNr + 2;
  405.       Line := Page.GetString(LineNr);
  406.     until Pos('</table>', Line) > 0;
  407.   end;
  408. end;
  409.  
  410.  
  411. (* ****************************************************************************
  412.  *
  413.  * FilmPub section
  414.  *
  415.  * ****************************************************************************)
  416.  
  417. function iPos (Substr: String; S: String): Integer;
  418. begin
  419.   Substr := AnsiLowerCase(Substr);
  420.   S := AnsiLowerCase(S);
  421.   Result := Pos(Substr, S);
  422. end;
  423.  
  424. function FormatText(T: String): String;
  425. var BeginPos: Integer;
  426. begin
  427.   BeginPos := iPos('  ', T);
  428.   while (BeginPos > 0 ) do
  429.   begin
  430.     Delete(T, BeginPos, 1);
  431.     BeginPos := iPos('  ', T);
  432.   end;
  433.  
  434.   T := StringReplace(T, #13#10, '');
  435.   T := StringReplace(T, '</p>', #13#10#13#10);
  436.   T := StringReplace(T, '</P>', #13#10#13#10);
  437.   T := StringReplace(T, '<br>', #13#10);
  438.   T := StringReplace(T, '<BR>', #13#10);
  439.   Result := T;
  440. end;
  441.  
  442. procedure AnalyzePageFilmPub(Address: string);
  443. var
  444.   Line, iLine, aLine, MovieTitle, MovieAddress: string;
  445.   BeginPos, EndPos: Integer;
  446. begin
  447.   Line := GetPage(Address);
  448.  
  449.   PickTreeClear;
  450.   PickTreeAdd('NalezenΘ filmy:', '');
  451.  
  452.   BeginPos := iPos('<a href="film.aspx', Line);
  453.   while (BeginPos > 0 ) do
  454.   begin
  455.     Line := Copy(Line, BeginPos, Length(Line));
  456.     EndPos := iPos('</a>', Line);
  457.     iLine := Copy(Line, 0, EndPos-1);
  458.     Line := Copy(Line, EndPos, Length(Line));
  459.  
  460.     BeginPos := iPos('"', iLine);
  461.     aLine := Copy(iLine, BeginPos+1, Length(iLine));
  462.     EndPos :=  iPos('"', aLine);
  463.     aLine := Copy(iLine, BeginPos+1, EndPos-1);
  464.     MovieAddress := BASEURL + '/' + aLine;
  465.     BeginPos := iPos('>', iLine);
  466.     MovieTitle := Trim(Copy(iLine, BeginPos+1, Length(iLine)));
  467.     PickTreeAdd(MovieTitle, MovieAddress);
  468.  
  469.     BeginPos := iPos('<a href="film.aspx', Line);
  470.   end;
  471.     if PickTreeExec(Address) then
  472.       AnalyzeMoviePageFilmPub(Address);
  473. end;
  474.  
  475.  
  476. procedure AnalyzeMoviePageFilmPub(Address: string);
  477. var
  478.   Line, iLine, Value, MovieAddress: string;
  479.   BeginPos, EndPos: Integer;
  480. begin
  481.   Line := GetPage(Address);
  482.  
  483.   BeginPos := iPos('Detail filmu', Line);
  484.   Line := Copy(Line, BeginPos, Length(Line));
  485.  
  486.   BeginPos := iPos('<h1>', Line);
  487.   EndPos := iPos('</h1>', Line);
  488.   iLine := Copy(Line, BeginPos, EndPos-BeginPos);
  489.   EndPos := iPos('<span>', iLine);
  490.   Value := Copy(iLine, 0, EndPos-1);
  491.   HTMLRemoveTags(Value);
  492.   HTMLDecode(Value);
  493.   if cTranslatedTitle then
  494.     SetField(fieldTranslatedTitle, Value);
  495.  
  496.   Value := Copy(iLine, EndPos, Length(iLine));
  497.   HTMLRemoveTags(Value);
  498.   HTMLDecode(Value);
  499.   if cOriginalTitle then
  500.     SetField(fieldOriginalTitle, Value);
  501.  
  502.   BeginPos := iPos('clanek.aspx?articleId=', Line);
  503.   Line := Copy(Line, BeginPos, Length(Line));
  504.   EndPos := iPos('"', Line);
  505.   MovieAddress := Copy(Line, 0, EndPos-1);
  506.  
  507.  
  508.   Line := GetPage(BASEURL + '/' + MovieAddress);
  509.   BeginPos := iPos('<p class="prolog"', Line);
  510.   Line := Copy(Line, BeginPos, Length(Line));
  511.   EndPos := iPos('</p>', Line);
  512.   Value := Copy(Line, 0, EndPos-1);
  513.   Value := FormatText(Value);
  514.   HTMLRemoveTags(Value);
  515.   HTMLDecode(Value);
  516.   if cDescription then
  517.     SetField(fieldDescription, Value);
  518.  
  519.   Line := Copy(Line, EndPos+Length('</p>'), Length(Line));
  520.   EndPos := iPos('</div>', Line);
  521.   Value := Copy(Line, 0, EndPos-1);
  522.  
  523.   Value := FormatText(Value);
  524.   HTMLRemoveTags(Value);
  525.   HTMLDecode(Value);
  526.   if cComments then
  527.     SetField(fieldComments, Value);
  528. end;
  529.  
  530. (* ****************************************************************************
  531.  *
  532.  * Main section
  533.  *
  534.  * ****************************************************************************)
  535.  
  536. begin
  537.   if CheckVersion(3,4,0) then
  538.   begin
  539.     MovieName := GetField(fieldOriginalTitle);
  540.     if MovieName = '' then
  541.       MovieName := GetField(fieldTranslatedTitle);
  542.     if Input('IMDb Import', 'Enter the title of the movie:', MovieName) then
  543.     begin
  544.       AnalyzePage('http://us.imdb.com/Tsearch?title='+UrlEncode(MovieName));
  545.       AnalyzePageFilmPub(BASEURL+'/hledej.aspx?findString='+UrlEncode(MovieName));
  546.       DisplayResults;
  547.     end;
  548.   end
  549.   else
  550.     ShowMessage('This script requires a newer version of Ant Movie Catalog (at least the version 3.4.0)');
  551. end.
  552.